package com.nami.dispatcher.infra.gateway;

import com.nami.dispatcher.domain.gateway.GrpGateway;
import com.nami.dispatcher.infra.cache.group.GroupCache;
import com.nami.gchat.client.api.GrpClient;
import com.nami.gchat.client.response.GroupDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class GrpGatewayImpl implements GrpGateway {

    @DubboReference
    GrpClient grpReadFacade;


    public GroupDTO getGrpInfoFromCache(String appKey, String grpId) {
        if (StringUtils.isBlank(appKey) || StringUtils.isBlank(grpId)) {
            return null;
        }
        GroupDTO groupVO = GroupCache.get(grpId);
        if (groupVO == null) {
            GroupDTO exist = grpReadFacade.getGrpInfo(appKey, grpId);
            if (exist == null) {
                return null;
            }
            GroupCache.add(grpId, exist);
            return exist.clone();
        }
        return groupVO;
    }
}
